<?php
/**
 * Created by PhpStorm.
 * User: sl
 * Date: 2018/7/4
 * Time: 9:49
 */

namespace app\index\controller;

use app\index\base_controller\BaseController;
use think\Db;
use think\Exception;
use think\facade\Session;

class User extends BaseController
{
    public function userList() {
        $roleList = Db::table('MNG_GZ_ROLE')->select();
        $this->assign('roleList',$roleList);
        return $this->fetch('user_list');
    }

    public function getUserList(){
        $limit = input('limit');
        $offset = input('offset');
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";
        if(empty($sort)){
            $sortStr = 'U.STAFF_ID desc';
        }
        $condition = null;
        $userId = input('user_id');
        $userName = input('user_name');
        $roleId = input('role_id');
        if(!empty($userId)){
            $condition[] = ['UR.USER_ID','like',"%$userId%"];
        }
        if(!empty($userName)){
            $condition[] = ['U.NAME','like',"%$userName%"];
        }
        if(!empty($roleId)){
            $condition[] = ['UR.ROLE_ID','=',$roleId];
        }

        try{
            $list = Db::view('MNG_GZ_USER_ROLE UR','*')
                ->view('EMPLOYEE U','NAME USER_NAME','U.STAFF_ID=UR.USER_ID')
                ->view('MNG_GZ_ROLE R','NAME ROLE_NAME','R.ROLE_ID=UR.ROLE_ID')
                ->view('DICT_COLLEGE UD','COLLEGE','UD.COLLEGE_ID=U.COLLEGE_ID','LEFT')
                ->where($condition)
                ->limit($offset,$limit)
                ->order($sortStr)
                ->select();

            $total = Db::view('MNG_GZ_USER_ROLE UR','*')
                ->view('EMPLOYEE U','NAME USER_NAME','U.STAFF_ID=UR.USER_ID')
                ->view('MNG_GZ_ROLE R','NAME ROLE_NAME','R.ROLE_ID=UR.ROLE_ID')
                ->view('MNG_UNITD UD','UD_NAME','UD.UD_ID=U.COLLEGE_ID','LEFT')
                ->where($condition)
                ->count();
            return ['rows'=>$list,'total'=>$total,'sql'=>Db::table('')->getLastSql(),'data'=>input()];
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    public function addPage(){
        $roleList = Db::table('MNG_GZ_ROLE')->select();
        $this->assign('roleList',$roleList);
        $belongList = Db::table('ASSET_DICT_BELONG')->select();
        $this->assign('belongList',$belongList);
        $collegeList = Db::table('DICT_COLLEGE')->select();
        $this->assign('collegeList',$collegeList);
        $this->view->engine->layout('layout_popup');
        return view('add_user_role');
    }

    public function addUserRole(){
        $userId = input('userId');
        $roleId = input('roleId');
        $belongIds = input('belongIds');
        $collegeIds = input('collegeIds');

        if(empty($userId)){
            return json_encode(['code'=>'0','msg'=>'请选择用户']);
        }
        if(empty($roleId)){
            return json_encode(['code'=>'0','msg'=>'请选择角色']);
        }
        if(empty($belongIds)){
            return json_encode(['code'=>'0','msg'=>'请勾选隶属单位数据范围']);
        }
        if(empty($collegeIds)){
            return json_encode(['code'=>'0','msg'=>'请勾选学院数据范围']);
        }

        $user = Db::table('EMPLOYEE')->where(['STAFF_ID'=>$userId])->find();
        if(empty($user)){
            return json_encode(['code'=>'0','msg'=>'该用户不存在']);
        }
        $role = Db::table('MNG_GZ_ROLE')->where(['ROLE_ID'=>$roleId])->find();
        if(empty($role)){
            return json_encode(['code'=>'0','msg'=>'该角色不存在']);
        }


        foreach ($belongIds as $belongId) {
            $belongData = Db::table('ASSET_DICT_BELONG')->where(['BELONG_ID'=>$belongId])->find();
            if (empty($belongData)) {
                return json_encode(['code'=>'0','msg'=>'隶属单位数据错误']);
            }
        }

        $userRole = Db::table('MNG_GZ_USER_ROLE')->where(['USER_ID'=>$userId,'ROLE_ID'=>$roleId])->find();
        if(!empty($userRole)){
            return json_encode(['code'=>'0','msg'=>'该用户已经具有该角色']);
        }

        $belongIdStr = implode(',',$belongIds);
        $belongDataCount = Db::table('ASSET_DICT_BELONG')->count();
        if (count($belongIds) == $belongDataCount) {
            $belongIdStr = '*';
        }

        foreach ($collegeIds as $collegeId) {
            $collegeData = Db::table('DICT_COLLEGE')->where(['COLLEGE_ID'=>$collegeId,'DEL_MARK'=>'0'])->find();
            if (empty($collegeData)) {
                return json_encode(['code'=>'0','msg'=>'学院数据错误']);
            }
        }
        $collegeIdStr = implode(',',$collegeIds);
        $collegeDataCount = Db::table('DICT_COLLEGE')->where(['DEL_MARK'=>'0'])->count();
        if (count($collegeIds) == $collegeDataCount) {
            $collegeIdStr = '*';
        }

        try{
            Db::table('MNG_GZ_USER_ROLE')->insert(['USER_ID'=>$userId,'ROLE_ID'=>$roleId,'BELONG_IDS'=>$belongIdStr,'COLLEGE_IDS'=>$collegeIdStr]);
            return json_encode(['code'=>'200','msg'=>'新增成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'新增失败：'.$e->getMessage()]);
        }
    }

    public function editPage() {
        $userId = input('userId');
        $roleId = input('roleId');

        $mngUserData = Db::view('MNG_GZ_USER_ROLE UR','*')
            ->view('EMPLOYEE U','NAME USER_NAME','U.STAFF_ID=UR.USER_ID')
            ->view('MNG_GZ_ROLE R','NAME ROLE_NAME','R.ROLE_ID=UR.ROLE_ID')
            ->view('DICT_COLLEGE UD','COLLEGE','UD.COLLEGE_ID=U.COLLEGE_ID','LEFT')
            ->where(['UR.USER_ID'=>$userId,'UR.ROLE_ID'=>$roleId])
            ->find();
        if (empty($mngUserData)) {
            return '查询用户数据范围信息错误';
        }

        $this->assign('mngUserData',$mngUserData);
        $this->view->engine->layout('layout_popup');
        return $this->fetch('user_edit');
    }

    public function edit() {
        $userId = input('userId');
        $roleId = input('roleId');
        $belongIds = input('belongIds');
        $collegeIds = input('collegeIds');

        if(empty($userId)){
            return json_encode(['code'=>'0','msg'=>'请选择用户']);
        }
        if(empty($roleId)){
            return json_encode(['code'=>'0','msg'=>'请选择角色']);
        }

        $userRoleData = Db::table('MNG_GZ_USER_ROLE')->where(['USER_ID'=>$userId,'ROLE_ID'=>$roleId])->find();
        if (empty($userRoleData)) {
            return json_encode(['code'=>'0','msg'=>'查询管理员失败']);
        }

        if(empty($belongIds)){
            return json_encode(['code'=>'0','msg'=>'请勾选隶属单位数据范围']);
        }
        if(empty($collegeIds)){
            return json_encode(['code'=>'0','msg'=>'请勾选学院数据范围']);
        }

        $user = Db::table('EMPLOYEE')->where(['STAFF_ID'=>$userId])->find();
        if(empty($user)){
            return json_encode(['code'=>'0','msg'=>'该用户不存在']);
        }
        $role = Db::table('MNG_GZ_ROLE')->where(['ROLE_ID'=>$roleId])->find();
        if(empty($role)){
            return json_encode(['code'=>'0','msg'=>'该角色不存在']);
        }


        foreach ($belongIds as $belongId) {
            $belongData = Db::table('ASSET_DICT_BELONG')->where(['BELONG_ID'=>$belongId])->find();
            if (empty($belongData)) {
                return json_encode(['code'=>'0','msg'=>'隶属单位数据错误']);
            }
        }


        $belongIdStr = implode(',',$belongIds);
        $belongDataCount = Db::table('ASSET_DICT_BELONG')->count();
        if (count($belongIds) == $belongDataCount) {
            $belongIdStr = '*';
        }

        foreach ($collegeIds as $collegeId) {
            $collegeData = Db::table('DICT_COLLEGE')->where(['COLLEGE_ID'=>$collegeId,'DEL_MARK'=>'0'])->find();
            if (empty($collegeData)) {
                return json_encode(['code'=>'0','msg'=>'学院数据错误']);
            }
        }
        $collegeIdStr = implode(',',$collegeIds);
        $collegeDataCount = Db::table('DICT_COLLEGE')->where(['DEL_MARK'=>'0'])->count();
        if (count($collegeIds) == $collegeDataCount) {
            $collegeIdStr = '*';
        }

        try{
            Db::table('MNG_GZ_USER_ROLE')->where(['USER_ID'=>$userId,'ROLE_ID'=>$roleId])->update(['BELONG_IDS'=>$belongIdStr,'COLLEGE_IDS'=>$collegeIdStr]);
            return json_encode(['code'=>'200','msg'=>'提交成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'提交失败：'.$e->getMessage()]);
        }
    }

    public function delete() {
        $userId = input('userId');
        $roleId = input('roleId');
        try{
            Db::table('MNG_GZ_USER_ROLE')
                ->where(['USER_ID'=>$userId,'ROLE_ID'=>$roleId])
                ->delete();
            return json_encode(['code'=>'200','msg'=>'删除成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>$e->getMessage()]);
        }
    }

    public function chooseUserPage() {
        $this->view->engine->layout('layout_popup');
        return $this->fetch('choose_user');
    }

    public function getEmployeeList() {
        $limit = input('limit');
        $offset = input('offset');
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";
        if(empty($sort)){
            $sortStr = 'STAFF_ID desc';
        }
        $condition = null;
        $userId = input('user_id');
        $userName = input('user_name');
        if(!empty($userId)){
            $condition[] = ['E.STAFF_ID','like',"%$userId%"];
        }
        if(!empty($userName)){
            $condition[] = ['E.NAME','like',"%$userName%"];
        }

        try{
            $list = Db::view('EMPLOYEE E','*')
                ->view('DICT_COLLEGE UD','COLLEGE','UD.COLLEGE_ID=E.COLLEGE_ID','LEFT')
                ->where($condition)
                ->limit($offset,$limit)
                ->order($sortStr)
                ->select();

            $total = Db::view('EMPLOYEE E','*')
                ->view('DICT_COLLEGE UD','COLLEGE','UD.COLLEGE_ID=E.COLLEGE_ID','LEFT')
                ->where($condition)
                ->count();
            return ['rows'=>$list,'total'=>$total,'sql'=>Db::table('')->getLastSql(),'data'=>input()];
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }
}